#!/usr/bin/python3

import postgresql, os

sql1 = "select id,name,upload_date from upload_tbl1 order by id"
db1 = postgresql.open('pq://user1:user049@localhost:5432/udb1')
ps1=db1.prepare(sql1)
ret1 = ps1()

print('\nid name upload_time')
for r1 in ret1:
    print('%d %s %s'%(r1[0],  r1[1],  str(r1[2])))
id_str1=input('选择id: ')
sql1 = ''
try:
    id1 = int(id_str1)
    if id1>0:
        sql1 = "select id,name,content from upload_tbl1 where id="+str(id1)
        sql2 = "delete from upload_tbl1 where id="+str(id1)
    cmd1=input('选择操作(o--导出,od--导出并删除): ')
    if id1>0 and (cmd1=='o' or cmd1=='od') :
        ps1=db1.prepare(sql1)
        ret2 = ps1()
        if len(ret2)>0:
            print(ret2[0][1]+str(ret2[0][0]))
            print( ret2[0][2] )
            filename1 = ret2[0][1]+str(ret2[0][0])+'.txt'
            fp1=open( filename1, mode='w' )
            fp1.write( ret2[0][2] )
            fp1.close()
            print('write file: '+filename1)
        
    if id1>0 and cmd1=='od':
        ps2=db1.prepare(sql2)
        ret3=ps2()
        if len(ret3)>0:
            for r1 in ret3:
                print(r1)
    cmd2=input('是否开始抽查(Y/n): ')
    if cmd2=='Y':
        os.system('python3 ./shizi.py '+filename1)
except:
    print('error')
db1.close()

